﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaoPortal.Areas.Admin.Models;

namespace TaoPortal.Areas.Admin.Controllers
{
    public class UserManageController : BaseController
    {
        //
        // GET: /UserManage/

        public ActionResult Index()
        {
            ViewBag.NavTitle = "用户管理";
            UserModel model = new UserModel();
            model.InitList();
            return View(model);
        }

        [HttpPost]
        public ActionResult AjaxGetList(UserModel model)
        {
            var data = model.GetUserList();
            return Content(SerializeJson(new { results = data.TotalItemCount, rows = data }));
        }

        public ActionResult AddUser()
        {
            UserModel model = new UserModel();
            model.InitList(false);
            model.InitProvince();
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddUser(UserModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.Entity.Password != model.Entity.ConfirmPassword)
                {
                    var modelerror = new ModelError("确认密码与密码不一致");
                    ModelState["Entity.ConfirmPassword"].Errors.Add(modelerror);
                }
                if (ModelState.IsValid)
                {
                    model.DoAdd();
                    return Content("<script>parent.SubmitSuccess()</script>");
                }
                else
                {
                    model.InitList(false);
                    model.InitProvince();
                    return PartialView(model);
                }
            }
            else
            {
                model.InitList(false);
                model.InitProvince();
                return PartialView(model);
            }
        }

        public ActionResult EditUser(string id)
        {
            UserModel model = new UserModel();
            model.InitEntity(id);
            model.InitList(false);
            model.InitProvince();
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditUser(UserModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.Entity.Password != model.Entity.ConfirmPassword)
                {
                    var modelerror = new ModelError("确认密码与密码不一致");
                    ModelState["Entity.ConfirmPassword"].Errors.Add(modelerror);
                }
                if (ModelState.IsValid)
                {
                    model.DoEdit();
                    return Content("<script>parent.SubmitSuccess()</script>");
                }
                else
                {
                    model.InitList(false);
                    model.InitProvince();
                    return PartialView(model);
                }
            }
            else
            {
                model.InitList(false);
                model.InitProvince();
                return PartialView(model);
            }
        }

        public ActionResult ShowDetail(string id)
        {
            UserModel model = new UserModel();
            model.InitEntity(id);
            model.InitList(false);
            return View(model);
        }
        [HttpPost]
        public ActionResult DeleteUser(string id)
        {
            UserModel model = new UserModel();
            model.Disable(id);
            return Content("true");
        }

        public ActionResult UserSetting()
        {
            UserSettingModel model = new UserSettingModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult UserSetting(UserSettingModel model)
        {
            return View(model);
        }

    }
}
